/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.annotation.Inner;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.UserTypeEnum;
import com.ykht.proj.coupon.dto.CheckSjNameDto;
import com.ykht.proj.coupon.dto.UserExtDto;
import com.ykht.proj.coupon.entity.CnBrand;
import com.ykht.proj.coupon.entity.CnCompany;
import com.ykht.proj.coupon.entity.CnShop;
import com.ykht.proj.coupon.entity.FxWaiterExt;
import com.ykht.proj.coupon.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@AllArgsConstructor
@RequestMapping("/user")
@Api(value = "user", tags = "登录用户扩展")
@Slf4j
public class CnUserExtController {

	private final CnUserExtService cnUserExtService;

	@ApiOperation(value = "根据用户类型和登录id获取用户扩展信息", notes = "根据用户类型和登录id获取用户扩展信息")
	@GetMapping("/getUserExt")
	public R getUserExt(@RequestParam("userType") Integer userType, @RequestParam("loginId") Long loginId) {
		Long sjId = SecurityUtils.getSjId();
		switch (UserTypeEnum.getThisByVal(userType)) {
			case company:
				CnCompany company = new CnCompany().selectById(sjId);
				return R.ok(company);
			case brand:
				CnBrand brand = new CnBrand().selectById(sjId);
				return R.ok(brand);
			case shop:
				CnShop shop = new CnShop().selectById(sjId);
				return R.ok(shop);
			case WAITER:
				FxWaiterExt waiterExt = new FxWaiterExt().selectOne(Wrappers.lambdaQuery(FxWaiterExt.class).eq(FxWaiterExt::getLoginId, loginId).eq(FxWaiterExt::getWorkState, 1));
				return R.ok(waiterExt);
			default:
				return R.ok();
		}

	}

/*	@Inner
	@ApiOperation(value = "根据用户类型和登录id获取用户扩展信息", notes = "根据用户类型和登录id获取用户扩展信息")
	@GetMapping("/getUserExt4Inner")
	public R getUserExt4Inner(@RequestParam("userType") Integer userType, @RequestParam("loginId") Long loginId) {
		return cnUserExtService.getUserExt(userType, loginId);
	}*/

	@Inner
	@ApiOperation(value = "根据用户类型和登录id获取用户扩展信息", notes = "根据用户类型和登录id获取用户扩展信息")
	@PostMapping("/getUserExtList")
	public R getUserExtList(@RequestBody UserExtDto userExtDto) {
		return cnUserExtService.getUserExt(userExtDto);
	}

	/**
	 * 获取多用户 的绑定数据（根据登录人角色）
	 *
	 * @param userReqList
	 * @return
	 */
	@ApiOperation(value = "获取多用户 的绑定数据（根据登录人角色）", notes = "获取多用户 的绑定数据（根据登录人角色）")
	@PostMapping("/getUserExtDtoMapLogingTyoe")
	public R<HashMap<Integer, List>> getUserExtDtoMapLogingTyoe(@RequestBody() List<UserExtDto> userReqList) {
		return cnUserExtService.getUserExtDtoMapLogingTyoe(userReqList);
	}

	/**
	 * 获取多用户 的绑定数据
	 *
	 * @return
	 */
	@ApiOperation(value = "验证切换用户是否可以切换")
	@PostMapping("/checkChangeSj")
	public R checkChangeSj(@RequestBody UserExtDto userExt) {
		log.info("->验证切换用户是否可以切换：{}", JSONUtil.toJsonStr(userExt));
		return cnUserExtService.changeSj(userExt);
	}

	@ApiOperation(value = "账号解绑商家")
	@PostMapping("/unBindSj")
	public R unBindSj(@RequestBody UserExtDto userExt) {
		log.info("->解绑商家：{}", JSONUtil.toJsonStr(userExt));
		return cnUserExtService.unBindSj(userExt);
	}
	@ApiOperation(value = "下架商家")
	@PostMapping("/offSj")
	public R offSj(@RequestBody UserExtDto userExt) {
		log.info("->下架商家：{}", JSONUtil.toJsonStr(userExt));
		return cnUserExtService.offSj(userExt);
	}

	@ApiOperation(value = "商家校验 （公司 品牌 门店）")
	@PostMapping("/checkSjName")
	public R checkSjName(@RequestBody CheckSjNameDto checkSjNameDto) {
		log.info("->商家校验 （公司 品牌 门店）：{}", JSONUtil.toJsonStr(checkSjNameDto));
		switch (UserTypeEnum.getThisByVal(checkSjNameDto.getUserType())) {
			case company:
				CnCompany company = new CnCompany().selectOne(Wrappers.lambdaQuery(CnCompany.class).eq(CnCompany::getName, checkSjNameDto.getName()));
				return R.ok(company == null ? true : company.getId().equals(checkSjNameDto.getId()) ? true : false);
			case brand:
				CnBrand brand = new CnBrand().selectOne(Wrappers.lambdaQuery(CnBrand.class).eq(CnBrand::getBrandName, checkSjNameDto.getName()));
				return R.ok(brand == null ? true : brand.getId().equals(checkSjNameDto.getId()) ? true : false);
			case shop:
				CnShop shop = new CnShop().selectOne(Wrappers.lambdaQuery(CnShop.class).eq(CnShop::getName, checkSjNameDto.getName()));
				return R.ok(shop == null ? true : shop.getId().equals(checkSjNameDto.getId()) ? true : false);
		}
		return R.ok();
	}


}
